package com.chinasofti.tcp03;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadSocket implements  Runnable {

    private Socket acceptSocket;

    public ThreadSocket(Socket acceptSocket) {
        this.acceptSocket = acceptSocket;
    }
    @Override
    public void run() {
        BufferedOutputStream bos =null;
        try {
            //网络中的流，从客户端获取数据
            BufferedInputStream bis = new BufferedInputStream(acceptSocket.getInputStream());
            //将图片，写到本地
        bos = new BufferedOutputStream(new FileOutputStream("java423\\" + UUID.randomUUID() + "copy.png"));

            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            //提示客户端  上传成功
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(acceptSocket.getOutputStream()));
            bw.write("上传成功！");
            bw.newLine();
            bw.flush();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            {
                try {
                    acceptSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
